//
//  RechargeViewController.m
//  CBT
//
//  Created by Mr.Zhang on 2018/11/24.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "RechargeViewController.h"
#import "RechargeListViewController.h"

#import "UserManager.h"

#import "NoDataView.h"

@interface RechargeViewController ()

@property (weak, nonatomic) IBOutlet UIBarButtonItem *listBarButton;
@property (weak, nonatomic) IBOutlet UIImageView *qrCodeImageView;
@property (weak, nonatomic) IBOutlet UILabel  *infoLabel;
@property (weak, nonatomic) IBOutlet UILabel  *ETHLabel;
@property (weak, nonatomic) IBOutlet UIButton *rootButton;

@property (weak, nonatomic) IBOutlet UILabel *walletAddressLabel;

@property (nonatomic, strong) NoDataView *noNetWorkView;
@property (nonatomic, copy) NSString *userAccountStr;

@end

@implementation RechargeViewController

#pragma mark - VC Life

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.tintAdjustmentMode = UIViewTintAdjustmentModeNormal;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.rootButton.backgroundColor = DefaultOrangeColor;
    
    [self startLoading];
    [self showInfos];
    [self getRecharge];
}

- (void)showInfos
{
    self.navigationItem.title = kLocalizedString(@"充值", nil);
    
    self.infoLabel.text =  [NSString stringWithFormat:@"%@ %@ %@",kLocalizedString(@"注意：请勿向以下地址充入除", nil),self.coinName,kLocalizedString(@"以外币种", nil)];
    self.ETHLabel.text  = [NSString stringWithFormat:@"%@ %@",self.coinName,kLocalizedString(@"收款码", nil)];
    [self.rootButton setTitle:kLocalizedString(@"复制收款地址", nil) forState:(UIControlStateNormal)];
    self.listBarButton.title     =  kLocalizedString(@"充值记录", nil);
    self.walletAddressLabel.text = [CBTLocalUserManager sharedInstance].walletAddress;
}

- (void)getRecharge {
    @weak(self)
    [UserManager getUserDoRechargeWithUserId:[CBTLocalUserManager sharedInstance].userId coinId:self.coinId block:^(id data, HttpRequestResult result, NSString *msg) {
        [self_weak_ stopLoading];
        [self_weak_.noNetWorkView removeFromSuperview];
        if (result == kHttpRequestResultNormal) {
            self_weak_.qrCodeImageView.image = [UIImage createNonInterpolatedUIImageWithString:[NSString stringWithFormat:@"%@",[data[@"data"]objectForKey:@"walletAddress"]] withSize:ScreenNarrowSide - 196];
            self_weak_.userAccountStr = [data[@"data"]objectForKey:@"walletAddress"];
            self_weak_.walletAddressLabel.text = [NSString stringWithFormat:@"%@",[data[@"data"]objectForKey:@"walletAddress"]];
        }
        else {
            [HFStatusView showFailureMessage:kLocalizedString(@"网络异常，请稍后重试", nil)];
            [self_weak_.view addSubview:self_weak_.noNetWorkView];
        }
    }];
}

- (IBAction)rechargeListAction:(UIBarButtonItem *)sender {
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    RechargeListViewController *vc = [story instantiateViewControllerWithIdentifier:@"rechargeListVC"];
    vc.showListType = 0;
    vc.coinId = self.coinId;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)rootBtnAction:(UIButton *)sender {
    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
    pboard.string = self.userAccountStr;
    [HFStatusView showSuccessMessage:kLocalizedString(@"已复制到剪贴板",nil)];
}

#pragma mark - Lazy

- (NoDataView *)noNetWorkView
{
    if (!_noNetWorkView) {
        @weak(self)
        _noNetWorkView = [[NoDataView alloc]initWithFrame:self.view.bounds type:(ShowType_NoNetWork) clickBlock:^{
            [self_weak_ startClearBgColorLoading];
            [self_weak_ getRecharge];
        }];
    }
    return _noNetWorkView;
}

@end
